Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
unist-util-map
Advanced tools
unist utility to create a new tree by mapping all nodes with a given function.
This is a small utility that helps you make new trees.
You can use this utility to create a new tree by mapping all nodes with a given
function.
Creating new trees like this can lead to performance problems, as it creates
new objects for every node.
When dealing with potentially large trees, and relatively few changes, use
unist-util-visit
(or
unist-util-visit-parents
) instead.
To remove certain nodes, you can also walk the tree with unist-util-visit
, or
use unist-util-filter
(clones the tree instead of
mutating) or unist-util-remove
(mutates).
To create trees, use unist-builder
.
This package is ESM only. In Node.js (version 14.14+ and 16.0+), install with npm:
npm install unist-util-map
In Deno with esm.sh
:
import {map} from 'https://esm.sh/unist-util-map@3'
In browsers with esm.sh
:
<script type="module">
import {map} from 'https://esm.sh/unist-util-map@3?bundle'
</script>
import {u} from 'unist-builder'
import {map} from 'unist-util-map'
const tree = u('tree', [
u('leaf', 'leaf 1'),
u('node', [u('leaf', 'leaf 2')]),
u('void'),
u('leaf', 'leaf 3')
])
const next = map(tree, (node) => {
return node.type === 'leaf'
? Object.assign({}, node, {value: 'CHANGED'})
: node
})
console.dir(next, {depth: null})
Yields:
{
type: 'tree',
children: [
{type: 'leaf', value: 'CHANGED'},
{type: 'node', children: [{type: 'leaf', value: 'CHANGED'}]},
{type: 'void'},
{type: 'leaf', value: 'CHANGED'}
]
}
👉 Note:
next
is a changed clone andtree
is not mutated.
This package exports the identifier map
.
There is no default export.
map(tree, mapFunction)
Create a new tree by mapping all nodes with the given function.
tree
(Node
)
— tree to mapmapFunction
(MapFunction
)
— function called with a node, its index, and its parent to produce a new
nodeNew mapped tree (Node
).
MapFunction
Function called with a node, its index, and its parent to produce a new node (TypeScript type).
node
(Node
)
— node to mapindex
(number
or undefined
)
— index of node
in parent
(if any)parent
(Node
or undefined
)
— parent of node
New mapped node (Node
).
The children on the returned node are not used. If the original node has children, those are mapped instead.
This package is fully typed with TypeScript.
It exports the additional type MapFunction
.
Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.
unist-util-filter
— create a new tree with all nodes that pass the given functionunist-util-flatmap
— create a new tree by expanding a node into manyunist-util-remove
— remove nodes from treesunist-util-select
— select nodes with CSS-like selectorsunist-util-visit
— walk treesunist-builder
— create treesSee contributing.md
in syntax-tree/.github
for
ways to get started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
FAQs
unist utility to create a new tree by mapping all nodes
The npm package unist-util-map receives a total of 70,560 weekly downloads. As such, unist-util-map popularity was classified as popular.
We found that unist-util-map demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.